﻿/// <reference path="TurboBase.js" />
/// <reference path="../General/General.js" />
/// <reference path="../General/Json.js" />


var _____a;

var $ListItem = TA.Turbo.Common.ListItem = function()
{
	if (Function.IsInherit(arguments)) return;

	this.Attributes = new Object();
	this.Items = new Array();
}
$ListItem.Inherit(BaseJsonSerializable);
$ListItem.Inherit(BaseJsonDeserializable);

if (_____a)
{
	$ListItem.prototype = new BaseJsonSerializable();
}

$ListItem.prototype.Text = "";
$ListItem.prototype.Value = "";
$ListItem.prototype.Style = "";
$ListItem.prototype.CssClass = "";
$ListItem.prototype.Attributes = new Object();
$ListItem.prototype.Enabled = true;
$ListItem.prototype.Visible = true;
$ListItem.prototype.Items = new Array();

$ListItem.prototype.AddAttributes = function AddAttributes(element)
{
	for (var attrName in this.Attributes)
	{
		element.setAttribute(attrName, this.Attributes[attrName]);
	}

	element.className = this.CssClass;
	element.style.cssText = this.Style;
	element.style["display"] = this.Visible ? element.style["display"] : "none";
	element.disabled = !this.Enabled;

	element.innerHTML = this.Text;
}
$ListItem.prototype.ReadAttributes = function ReadAttributes(element)
{
	this.Attributes = new Object();
	var ea = element.attributes, eal = ea.length, value;
	for (var i = 0; i < eal; ++i)
	{
		var a = ea[i];

		value = null;
		try
		{
			value = element.getAttribute(a.nodeName);
		}
		catch(ex)
		{
		}
		if (value != null)
			this.Attributes[a.nodeName] = value;
	}

	this.CssClass = element.className;
	this.Style = element.style.cssText;
	this.Visible = element.style["display"] != "none";
	this.Enabled = !element.disabled;
}

$ListItem.prototype.get_IsDirty = function get_IsDirty()
{
	var hasAttributes = false;
	for (var attrName in this.Attributes)
	{
		hasAttributes = true;
		break;
	}
	return !String.IsNullOrEmpty(this.Text)
		|| !String.IsNullOrEmpty(this.Value)
		|| !String.IsNullOrEmpty(this.Style)
		|| !String.IsNullOrEmpty(this.CssClass)
		|| !this.Enabled
		|| !this.Visible
		|| hasAttributes
		|| this.Items.length > 0
		;
}
$ListItem.prototype.SerializeContents = function SerializeContents(jw)
{
	if (______a)
		jw = new JsonWriter();

	if (!String.IsNullOrEmpty(this.Text))
		jw.Property("T", this.Text);
	if (!String.IsNullOrEmpty(this.Value))
		jw.Property("V", this.Value);
	if (!String.IsNullOrEmpty(this.Style))
		jw.Property("S", this.Style);
	if (!String.IsNullOrEmpty(this.CssClass))
		jw.Property("C", this.CssClass);
	if (!this.Enabled)
		jw.Property("E", this.Enabled);
	if (!this.Visible)
		jw.Property("L", this.Visible);

	if (this.Items.length > 0)
	{
		jw.PropertyName("I");
		jw.StartArray();

		for (var iItem = 0; iItem < this.Items.length; ++iItem)
		{
			jw.Value(this.Items[iItem]);
		}

		jw.EndArray();
	}


	var hasAttributes = false;
	for (var attrName in this.Attributes)
	{
		hasAttributes = true;
		break;
	}
	if (hasAttributes)
	{
		jw.Property("A", this.Attributes);
	}
}
$ListItem.prototype.DeserializeContent = function DeserializeContent(prShortName, value)
{
	switch (prShortName)
	{
		case "T": this.Text = value; break;
		case "V": this.Value = value; break;
		case "S": this.Style = value; break;
		case "C": this.CssClass = value; break;
		case "E": this.Enabled = value; break;
		case "L": this.Visible = value; break;
		case "A": this.Attributes = value; break;
		case "I":
			this.Items = new Array();

			for (var iItem = 0; iItem < value.length; ++iItem)
			{
				var item = new $ListItem();
				item.Deserialize(value[iItem]);
				this.Items.push(item);
			}

			break;
		default: Throw("Unrecognized property short name " + prShortName + " with value " + value);
	}
}




















var $ListItemCollection = TA.Turbo.Common.ListItemCollection = function ListItemCollection()
{
	this.JsonCompatibleCollection();
}
$ListItemCollection.Inherit(JsonCompatibleCollection);
if (_____a)
{
	$ListItemCollection.prototype = new JsonCompatibleCollection();
}

$ListItemCollection.prototype.CreateOneItem = function CreateOneItem()
{
	return new $ListItem();
}
